//
//  M4TabBarViewController.swift
//  M4
//
//  Created by YuChuang on 2024/8/6.
//

import UIKit
import SnapKit

class M4TabBarViewController: UITabBarController {
    
    lazy private var M4LineView: UIView = {
        let view = UIView()
        view.backgroundColor = COLOR_E8E8E8
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBar.backgroundColor = .white
        M4ChildNavItem(con: M4HomeViewController(), title: "Página de inico", normalImg: "tab_icon_home_nor", selectedImg: "tab_icon_home_pre")
        M4ChildNavItem(con: M4PagoViewController(), title: "Canal de pago", normalImg: "tab_icon_repayment_nor", selectedImg: "tab_icon_repayment_pre")
        M4ChildNavItem(con: M4MineViewController(), title: "Mi cuenta", normalImg: "tab_icon_user_nor", selectedImg: "tab_icon_user_pre")
        self.tabBar.addSubview(M4LineView)
        M4LineView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    

    private func M4ChildNavItem(con: UIViewController, title: String, normalImg: String, selectedImg: String) {
        con.tabBarItem.title = title
        con.tabBarItem.image = UIImage(named: normalImg)?.withRenderingMode(.alwaysOriginal)
        con.tabBarItem.selectedImage = UIImage(named: selectedImg)?.withRenderingMode(.alwaysOriginal)
        con.tabBarItem.setTitleTextAttributes([.foregroundColor:COLOR_999999, .font: UIFont.systemFont(ofSize: 11, weight: .medium)], for: .normal)
        con.tabBarItem.setTitleTextAttributes([.foregroundColor:COLOR_00479D, .font: UIFont.systemFont(ofSize: 11, weight: .medium)], for: .selected)
        let nav = M4UINavigationController.init(rootViewController: con)
        addChild(nav)
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if M4UserDataManager.m4UserShare.M4Token == "" {
            AppDelegate.M4JumpLoginPage()
        }
    }
}
